﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eTerm.MsSql.DbProvider;
using eTerm.Platform.Common;

namespace eTerm.Platform.Web
{
    public partial class UserEdit : BaseEntityPage<SysUser>
    {
        protected override void InitPageLoad()
        {
            SysUser model = Get();

            if (model == null) {
                tbCompanyCode.Enabled = tbUserNo.Enabled = true;
                btnAssign.Visible = false;
                return;
            }

            SysCompany company = SysCompany.FirstOrDefault(l=>l.CompanyCode == model.CompanyCode );
            tbCompanyCode.Text = company.CompanyCode;
            tbCompanyName.Text = company.CompayName;
            tbDescription.Text = company.Description;
            hndUID.Value = model.UserId.ToString();
            tbEmail.Text = model.Email;
            tbDisplayName.Text = model.DisplayName;
            tbMobile.Text = model.Mobile;
            tbUserNo.Text = model.UserNo;

        }

        /// <summary>
        /// Gets the page loading text.
        /// </summary>
        /// <value>The page loading text.</value>
        protected override string PageLoadingText
        {
            get
            {
                return @"正在处理用户信息请求";
            }
        }

        protected override void InitEasyUI()
        {
            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnSave.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-save'  
}});  
", btnSave.ClientID), true);
            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnAssign.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-sum'  
}});  
", btnAssign.ClientID), true);
            int userID =string.IsNullOrEmpty(Request["ID"])?0: int.Parse(Request["ID"]);
            btnAssign.OnClientClick = string.Format(@"$.jBox('post:PermissionTree.aspx', {{ajaxData:{{ ID:'{0}' }},title:'权限分配',buttons:{{ '关闭':true }}, bottomText: '为用户分配权限' }}); return false;", userID);
        }

        protected override SysUser Get()
        {
            int userID = int.Parse(Request["ID"]);
            return SysUser.FirstOrDefault(l => l.UserId == userID);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SysUser userModel = null;
            if (string.IsNullOrEmpty(hndUID.Value))
            {
                new SysCompany() { 
                 CompanyCode = tbCompanyCode.Text.Trim(),
                  CompayName = tbCompanyName.Text.Trim(),
                   CreateDate =DateTime.Now,
                    Description = tbDescription.Text.Trim()
                }.Save();
                userModel = new SysUser()
                {
                    UserNo = tbUserNo.Text.Trim(),
                    Password = Encrypt.MD5Encrypt(tbPassword.Text.Trim()),
                    ParentId = UID,
                    Mobile = tbMobile.Text.Trim(),
                    Email = tbEmail.Text.Trim(),
                    DisplayName = tbDisplayName.Text.Trim(),
                    CompanyCode = tbCompanyCode.Text.Trim(),
                    Description = string.Empty,
                };
            }
            else
            {
                SysCompany company = SysCompany.FirstOrDefault(l => l.CompanyCode == tbCompanyCode.Text.Trim());
                company.CompayName = tbCompanyName.Text.Trim();
                company.Description = tbDescription.Text.Trim();
                company.Save();
                userModel = SysUser.FirstOrDefault(l => l.UserId == int.Parse(hndUID.Value));
                userModel.Password = string.IsNullOrEmpty(tbPassword.Text.Trim()) ? userModel.Password : Encrypt.MD5Encrypt(tbPassword.Text.Trim());
                userModel.Mobile = tbMobile.Text.Trim();
                userModel.DisplayName = tbDisplayName.Text.Trim();
                userModel.Email = tbEmail.Text.Trim();
                userModel.CompanyCode = tbCompanyCode.Text.Trim();
            }
            userModel.Save();
        }
    }
}
